<?php

namespace App\Http\Controllers\YApi;

//use Illuminate\Http\Request;
use Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

use Session;
use Redirect;

use App\Models\Book;
use App\Models\Bags;
use App\Models\BagsLiuyan;
use App\Models\UsersReg;

class ShopBookslistController extends Controller
{

  public function shopbookslist()
  {
        //书城里图书的列表
        //转让方式 -- J借阅，E交换，S出售，Z赠送，B以上均可
        $otype = request::input('type'); 
        $pre_page = 15;
        if($otype == ''){
        //显示全部商品
        $bags = Bags::orderBy('add_time', 'desc')->paginate($pre_page);
      }else{
            //显示转让方式的商品
            $bags = Bags::orderBy('add_time', 'desc')->where('otype', $otype)->OrWhere('otype', 'B')->paginate($pre_page);
      }

      foreach($bags as $bag)
      {
            // 为了得到图书的信息
            // $bag['books'] = $bag->hasOnebooks;
            $bag['books'] = Book::find($bag->book_id);
      }
      foreach($bags as $bag)
      {
            // 为了得到用户的信息
            // $bag['users'] = $bag->hasTcUsersReg;
            $bag['users'] = UsersReg::find($bag->uid);
      }

      foreach($bags as $bag)
      {
            //获得对应的出售书籍的留言数量
            $bag['liuyancount'] = BagsLiuyan::where('bagsid', $bag->id)->count();
      }

        //return $bags;

        return  $bags;
  }
    
}